﻿
using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

public class DataSetHelper
{



    /// <summary>
    /// 创建数据集 spInfoData 桌面DataTable 格式
    /// </summary>
    /// <returns></returns>
    public DataTable CreateSpInfoDataData()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("hh");
        dt.Columns.Add("spbh");
        dt.Columns.Add("tmh");
        dt.Columns.Add("spmc");
        dt.Columns.Add("gg");
        dt.Columns.Add("dw");
        dt.Columns.Add("sl");
        dt.Columns.Add("lsj");

        dt.Columns.Add("pfj");
        dt.Columns.Add("ssje");
        dt.Columns.Add("zke");
        dt.Columns.Add("zkl");
        dt.Columns.Add("zhj");//折后价

        dt.Columns.Add("bdz", System.Type.GetType("System.Boolean"));
        dt.Columns.Add("gysmc");
        dt.Columns.Add("cjhh");
        dt.Columns.Add("jj");
        return dt;
    }


    public static DataSet FilledDs(DataSet ds, DataTable dt)
    {
        if (ds.Tables.Count > 0)
        {
            object[] obj = new object[dt.Columns.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i].ItemArray.CopyTo(obj, 0);
                ds.Tables[0].Rows.Add(obj);
            }
        }
        else
        {
            DataTable ndt = dt.Copy();
            ds.Tables.Add(ndt);
        }
        return ds;
    }


    #region 序列化 反序列化




    /// 序列化DataTable
    /// </summary>
    /// <param name="pDt">包含数据的DataTable</param>
    /// <returns>序列化的DataTable</returns>
    public static string SerializeDataTableXml(DataTable pDt, string pdtname)
    {
        // 序列化DataTable
        pDt.TableName = pdtname;
        StringBuilder sb = new StringBuilder();
        XmlWriter writer = XmlWriter.Create(sb);
        XmlSerializer serializer = new XmlSerializer(typeof(DataTable));
        serializer.Serialize(writer, pDt);
        writer.Close();
        return sb.ToString();
    }




    ///序列化转化为二进制数组的DataSet
    public static byte[] GetDataSetSurrogateZipBYtes(DataSet DS)
    {
        DataSetSurrogate dss = new DataSetSurrogate(DS);
        ///二进制方式方式序列化

        BinaryFormatter ser = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();
        ser.Serialize(ms, dss);
        byte[] buffer = ms.ToArray();
        ///调用压缩方法

        byte[] Zipbuffer = Compress(buffer);
        return Zipbuffer;
    }
    ///压缩二进制文件
    private static byte[] Compress(byte[] data)
    {
        MemoryStream ms = new MemoryStream();
        Stream zipStream = null;
        zipStream = new GZipStream(ms, CompressionMode.Compress, true);

        ///从指定的字节数组中将压缩的字节写入基础流

        zipStream.Write(data, 0, data.Length);
        zipStream.Close();
        ms.Position = 0;
        byte[] Compressed_Data = new byte[ms.Length];
        ms.Read(Compressed_Data, 0, int.Parse(ms.Length.ToString()));
        return Compressed_Data;
    }



    ///将压缩后的二进制数组解压

    public static byte[] Decompress(byte[] data)
    {      ///data参数为压缩后的二进制数组
        try
        {
            MemoryStream ms = new MemoryStream(data);
            Stream zipStream = null;
            zipStream = new GZipStream(ms, CompressionMode.Decompress);
            byte[] dc_data = null;
            dc_data = EtractBytesFormStream(zipStream, data.Length);
            return dc_data;

            ///返回解压后的二进制数组
        }
        catch
        {
            return null;
        }
    }


    public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
    {
        try
        {
            byte[] data = null;
            int totalBytesRead = 0;
            while (true)
            {
                Array.Resize(ref data, totalBytesRead + dataBlock + 1);
                int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
                if (bytesRead == 0)
                {
                    break;
                }
                totalBytesRead += bytesRead;
            }
            Array.Resize(ref data, totalBytesRead);
            return data;
        }
        catch
        {
            return null;
        }
    }


    /// 将二进制文件反序列化后转化为DataSet

    public DataSet GetDatasetFromByte(byte[] ZipByte)
    {
        byte[] buffer = Decompress(ZipByte);
        BinaryFormatter ser = new BinaryFormatter();
        DataSetSurrogate dss;
        dss = (DataSetSurrogate)ser.Deserialize(new MemoryStream(buffer));
        DataSet DS = dss.ConvertToDataSet();
        return DS;

    }


    /// <summary>
    /// 反序列化DataTable
    /// </summary>
    /// <param name="pXml">序列化的DataTable</param>
    /// <returns>DataTable</returns>
    public static DataTable DeserializeDataTable(string pXml)
    {

        StringReader strReader = new StringReader(pXml);
        XmlReader xmlReader = XmlReader.Create(strReader);
        XmlSerializer serializer = new XmlSerializer(typeof(DataTable));

        DataTable dt = serializer.Deserialize(xmlReader) as DataTable;
        return dt;
    }


    #endregion

    #region 销售运算

    /// <summary>
    /// 实收金额合计
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public decimal GetAllMoney(List<SalesEntity> WoList)
    {
        decimal allMoney = 0;
        if (WoList != null)
            allMoney = WoList.Sum(x => x.ssje);
        return allMoney;
    }

    /// <summary>
    /// 金额合计(未打折时)
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public decimal GetWDZAllMoney(List<SalesEntity> WoList)
    {
        decimal allMoney = 0;
        if (WoList != null)
            allMoney = WoList.Sum(x => x.ssje * x.sl);
        return allMoney;
    }

    /// <summary>
    /// 成本价金额合计
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public decimal GetCBJAllMoney(List<SalesEntity> WoList)
    {
        decimal allMoney = 0;
        if (WoList != null)
            allMoney = WoList.Sum(x => x.jj * x.sl);
        return allMoney;
    }


    /// <summary>
    /// 折扣额 合计
    /// </summary>
    /// <returns></returns>
    public decimal GetZKEAllMoney(List<SalesEntity> WoList)
    {
        decimal allMoney = 0;
        if (WoList != null)
            allMoney = WoList.Sum(x => x.zke * x.sl);
        return allMoney;
    }

    /// <summary>
    /// 数量合计
    /// </summary>
    /// <returns></returns>
    public decimal GetAllSl(List<SalesEntity> WoList)
    {
        decimal allSl = 0;
        decimal sl = 0;

        if (WoList != null)
            allSl = WoList.Sum(x => x.sl);
        return allSl;
    }

    #endregion


}

